package com.module.overview.ui.editCardList

import android.view.View
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.base.AppActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyEvents
import com.common.app.data.bean.overView.CardSettingListModel
import com.common.app.data.bean.overView.CardSettingModel
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.showToast
import com.common.base.utils.StatusBarUtils
import com.common.base.widget.animator.CustomAnimation1
import com.jeremyliao.liveeventbus.LiveEventBus
import com.module.overview.R
import com.module.overview.databinding.OverviewActivityCardEditBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/06/15
 * <p>
 * 描述：卡片编辑界面，支持拖拽排序和开关状态切换
 * <p>
 * 修订历史：
 */
@Route(path = RouterHub.ROUTER_OVERVIEW_EDIT_CARD)
class EditCardActivity : AppActivity<OverviewActivityCardEditBinding, EditCardViewModel>(), View.OnClickListener, OverviewEditCardListAdapter.OnStartDragListener  {

    override fun getViewModel() = EditCardViewModel::class.java
    override fun getViewContentBinding() = OverviewActivityCardEditBinding.inflate(layoutInflater)
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    // 适配器
    private val adapter by lazy { OverviewEditCardListAdapter(this) }
    
    // ItemTouchHelper实例
    private lateinit var itemTouchHelper: ItemTouchHelper
    

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)
        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(resources.getString(R.string.overview_ac_card_edit_title))
        headTitleUtils.setLineStyle(10)

        // 初始化RecyclerView
        mViewContentBinding.recyclerView.layoutManager = LinearLayoutManager(this)
        
        // 设置适配器
        adapter.apply {
            adapterAnimation = CustomAnimation1()
            isAnimationFirstOnly = false
            setOnSwitchChangeListener { position, isEnabled ->
                // 处理开关状态变化
                val card = adapter.data[position]
                card.isEnabled = isEnabled
//                showToast("${card.name}已${if(isEnabled) "启用" else "禁用"}")
//                // 在实际应用中，这里应该保存状态到数据库或通过ViewModel更新
//                mViewModel.updateCardStatus(card)
            }
        }

        mViewContentBinding.recyclerView.adapter = adapter
        
        // 初始化ItemTouchHelper
        val callback = ItemTouchHelperCallback(adapter)
        itemTouchHelper = ItemTouchHelper(callback)
        itemTouchHelper.attachToRecyclerView(mViewContentBinding.recyclerView)
    }

    override fun initEvents() {
        super.initEvents()

        mViewModel.cardSettings.observe(this) {
            // 设置数据
            adapter.setList(it as MutableList<CardSettingModel>?)
        }

        mViewContentBinding.tvSaveBtn.setOnClickListener(onClick = this)
    }

    override fun lazyInitData() {
        super.lazyInitData()
        // 在实际应用中，这里应该从ViewModel加载数据
         mViewModel.loadCardSettings()
    }

    override fun onClick(v: View?) {
        when (v) {
            // 处理点击事件
            mViewContentBinding.tvSaveBtn->{
                LiveEventBus.get(KeyEvents.KEY_OV_REFRESH_CONFIG, CardSettingListModel::class.java).post(
                    mViewModel.cardSettings.value?.let { CardSettingListModel(it) })
                finish()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        // 在实际应用中，这里可以保存最终的排序和状态
        // mViewModel.saveCardSettings(adapter.data)
    }

    override fun onStartDrag(viewHolder: BaseViewHolder) {
        // 开始拖拽
        itemTouchHelper.startDrag(viewHolder)
    }

}